<?php

namespace Yolo\Components\Cache;

use ReflectionException;
use Yolo\ComponentManager\AbstractComponent;
use Yolo\Di\DI;
use Yolo\Di\Errors\CircularDependencyException;
use Yolo\Di\Errors\InvalidAttributeException;
use Yolo\Di\Errors\ParameterTypeEmptyException;
use Yolo\Service\ApplicationContainer;

/**
 * Class CacheComponent.
 */
class CacheComponent extends AbstractComponent
{
    public function __construct(
        private readonly ApplicationContainer $container
    ){}

    /**
     * @return void
     * @throws ReflectionException
     * @throws CircularDependencyException
     * @throws ParameterTypeEmptyException|InvalidAttributeException
     */
    public function register(): void
    {
        // 设置缓存驱动
        switch ($this->container->getConfig()->get('cache.driver', 'memory')) {
            case 'redis':
                DI::bind(CacheInterface::class, RedisCache::class);
                break;

            default:
                DI::bind(CacheInterface::class, MemoryCache::class);
                break;
        }

        DI::use(CacheFactory::class);
    }
}
